﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Threading;

namespace BeepMaker
{
    public partial class Config : Form
    {
        public Config()
        {
            InitializeComponent();
        }



        private void Config_Load(object sender, EventArgs e)
        {
            nudAmplitude.Minimum = tbAmplitude.Minimum = Consts.MIN_AMPLITUDE;
            nudAmplitude.Maximum = tbAmplitude.Maximum = Consts.MAX_AMPLITUDE;

            nudDuration.Minimum = tbDuration.Minimum = Consts.MIN_DURATION_IN_MILLISECONDS;
            nudDuration.Maximum = tbDuration.Maximum = Consts.MAX_DURATION_IN_MILLISECONDS;

            nudFrequency.Minimum = tbFrequency.Minimum = Consts.MIN_FREQUENCY_IN_HERTZ;
            nudFrequency.Maximum = tbFrequency.Maximum = Consts.MAX_FREQUENCY_IN_HERTZ;

            nudInterval.Minimum = tbInterval.Minimum = Consts.MIN_INTERVAL_IN_MILLISECONDS;
            nudInterval.Maximum = tbInterval.Maximum = Consts.MAX_INTERVAL_IN_MILLISECONDS;

            tbAmplitude.LargeChange = (tbAmplitude.Maximum - tbAmplitude.Minimum) / 100;
            tbDuration.LargeChange = (tbDuration.Maximum - tbDuration.Minimum) / 100;
            tbFrequency.LargeChange = (tbFrequency.Maximum - tbFrequency.Minimum) / 100;
            tbInterval.LargeChange = (tbInterval.Maximum - tbInterval.Minimum) / 100;

            AppSettingsReader appSetReader = new System.Configuration.AppSettingsReader();
            int amplitude = (int)appSetReader.GetValue("amplitude", typeof(int));
            amplitude = Math.Min(Math.Max(Consts.MIN_AMPLITUDE, amplitude), Consts.MAX_AMPLITUDE);

            int frequency = (int)appSetReader.GetValue("frequencyInHertz", typeof(int));
            frequency = Math.Min(Math.Max(Consts.MIN_FREQUENCY_IN_HERTZ, frequency), Consts.MAX_FREQUENCY_IN_HERTZ);

            int duration = (int)appSetReader.GetValue("durationInMilliSeconds", typeof(int));
            duration = Math.Min(Math.Max(Consts.MIN_DURATION_IN_MILLISECONDS, duration), Consts.MAX_DURATION_IN_MILLISECONDS);

            int interval = (int)appSetReader.GetValue("intervalInMilliSeconds", typeof(int));
            interval = Math.Min(Math.Max(Consts.MIN_INTERVAL_IN_MILLISECONDS, interval), Consts.MAX_INTERVAL_IN_MILLISECONDS);

            tbAmplitude.Value = amplitude;
            tbDuration.Value = duration;
            tbFrequency.Value = frequency;
            tbInterval.Value = interval;
        }

        private void ChangeValue(TrackBar obj, decimal value)
        {
            obj.ValueChanged -= OnValueChanged;
            obj.Value = (int)value;
            if (obj.Name.Contains("Duration"))
                lblTimeDuration.Text = new TimeSpan(0, 0, 0, 0, obj.Value).ToString();
            else if (obj.Name.Contains("Interval"))
                lblTimeInterval.Text = new TimeSpan(0, 0, 0, 0, obj.Value).ToString();
            obj.ValueChanged += OnValueChanged;
        }

        private void ChangeValue(NumericUpDown obj, int value)
        {
            obj.ValueChanged -= OnValueChanged;
            obj.Value = value;
            if (obj.Name.Contains("Duration"))
                lblTimeDuration.Text = GetTimeString(new TimeSpan(0, 0, 0, 0, (int)obj.Value));
            else if (obj.Name.Contains("Interval"))
                lblTimeInterval.Text = GetTimeString(new TimeSpan(0, 0, 0, 0, (int)obj.Value));
            obj.ValueChanged += OnValueChanged;
        }

        private string GetTimeString(TimeSpan ts)
        {
            StringBuilder sb = new StringBuilder();
            if (ts.Hours > 0)sb.AppendFormat("{0:00} h", ts.Hours);
            if (ts.Minutes > 0)
            {
                if (ts.Hours > 0)sb.Append(" - ");
                sb.AppendFormat("{0:00} m", ts.Minutes);
            }
            if (ts.Seconds > 0)
            {
                if (ts.Hours > 0 || ts.Minutes > 0)sb.Append(" - ");
                sb.AppendFormat("{0:00}.{1:000} s", ts.Seconds, ts.Milliseconds);
            }
            return sb.ToString();
        }

        private void OnValueChanged(object sender, EventArgs e)
        {
            if (sender is NumericUpDown)
            {
                NumericUpDown ctrl = (NumericUpDown)sender;
                if (ctrl.Name.Contains("Duration"))
                    ChangeValue(tbDuration, (int)(ctrl.Value));
                else if (ctrl.Name.Contains("Amplitude"))
                    ChangeValue(tbAmplitude, (int)(ctrl.Value));
                else if (ctrl.Name.Contains("Frequency"))
                    ChangeValue(tbFrequency, (int)(ctrl.Value));
                else if (ctrl.Name.Contains("Interval"))
                    ChangeValue(tbInterval, (int)(ctrl.Value));
            }
            else if (sender is TrackBar)
            {
                TrackBar ctrl = (TrackBar)sender;
                if (ctrl.Name.Contains("Duration"))
                    ChangeValue(nudDuration, ctrl.Value);
                else if (ctrl.Name.Contains("Amplitude"))
                    ChangeValue(nudAmplitude, ctrl.Value);
                else if (ctrl.Name.Contains("Frequency"))
                    ChangeValue(nudFrequency, ctrl.Value);
                else if (ctrl.Name.Contains("Interval"))
                    ChangeValue(nudInterval, ctrl.Value);
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            AppSettingsWriter writer = new AppSettingsWriter();
            writer.SetNewValue("amplitude", tbAmplitude.Value.ToString());
            writer.SetNewValue("frequencyInHertz", tbFrequency.Value.ToString());
            writer.SetNewValue("durationInMilliSeconds", tbDuration.Value.ToString());
            writer.SetNewValue("intervalInMilliSeconds", tbInterval.Value.ToString());
            writer.Save();

            Application.Restart();
        }

        private void nudDuration_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsPunctuation(e.KeyChar) ||
                e.KeyChar == '-')
                e.Handled = true;
        }

        private void nudDuration_Enter(object sender, EventArgs e)
        {
            NumericUpDown nud = (NumericUpDown)sender;
            nud.Select(0, nud.ToString().Length);
        }
    }
}
